/**
 * WMS订单管理API服务类
 */
import request from "@/utils/http";

/**
 * 基础订单API服务
 */
export class WmsOrderService {
  /**
   * 获取订单列表
   */
  static getOrderList(params?: WMS.Order.OrderQueryParams) {
    return request.get<
      Api.Http.BaseResponse<{
        rows: WMS.Order.BaseOrderInfo[];
        total: number;
      }>
    >({
      url: "/wms/order/list",
      params,
    });
  }

  /**
   * 获取订单详情
   */
  static getOrderDetail(id: number) {
    return request.get<WMS.Order.BaseOrderInfo>({
      url: `/wms/order/${id}`,
    });
  }

  /**
   * 删除订单
   */
  static deleteOrder(id: number) {
    return request.del<void>({
      url: `/wms/order/${id}`,
    });
  }

  /**
   * 批量删除订单
   */
  static batchDeleteOrders(ids: number[]) {
    return request.del<void>({
      url: "/wms/order/batch",
      data: { ids },
    });
  }

  /**
   * 获取订单统计数据
   */
  static getOrderStatistics(params?: {
    warehouseId?: number;
    orderType?: WMS.Order.OrderType;
    dateRange?: string[];
  }) {
    return request.get<WMS.Order.OrderStatistics>({
      url: "/wms/order/statistics",
      params,
    });
  }

  /**
   * 导出订单数据
   */
  static exportOrderData(params?: WMS.Order.OrderQueryParams) {
    return request.get<Blob>({
      url: "/wms/order/export",
      params,
      responseType: "blob",
    });
  }
}

/**
 * 入库单API服务
 */
export class WmsReceiptOrderService {
  /**
   * 获取入库单列表
   */
  static getReceiptOrderList(params?: WMS.Order.OrderQueryParams) {
    return request.get<WMS.Order.ReceiptOrderListResponse>({
      url: "/wms/receiptOrder/list",
      params,
    });
  }

  /**
   * 获取入库单详情
   */
  static getReceiptOrderDetail(id: number) {
    return request.get<WMS.Order.ReceiptOrderInfo>({
      url: `/wms/receiptOrder/${id}`,
    });
  }

  /**
   * 创建入库单
   */
  static createReceiptOrder(
    data: Omit<
      WMS.Order.ReceiptOrderInfo,
      "id" | "orderNo" | "createTime" | "updateTime"
    >,
  ) {
    return request.post<WMS.Order.ReceiptOrderInfo>({
      url: "/wms/receiptOrder",
      data,
    });
  }

  /**
   * 更新入库单
   */
  static updateReceiptOrder(
    id: number,
    data: Partial<WMS.Order.ReceiptOrderInfo>,
  ) {
    return request.put<WMS.Order.ReceiptOrderInfo>({
      url: `/wms/receiptOrder/${id}`,
      data,
    });
  }

  /**
   * 删除入库单
   */
  static deleteReceiptOrder(id: number) {
    return request.del<void>({
      url: `/wms/receiptOrder/${id}`,
    });
  }

  /**
   * 确认入库单
   */
  static confirmReceiptOrder(id: number) {
    return request.post<void>({
      url: `/wms/receiptOrder/${id}/confirm`,
    });
  }

  /**
   * 开始入库作业
   */
  static startReceiptWork(id: number) {
    return request.post<void>({
      url: `/wms/receiptOrder/${id}/start`,
    });
  }

  /**
   * 完成入库作业
   */
  static completeReceiptWork(
    id: number,
    data: {
      details: Array<{
        id: number;
        actualQuantity: number;
        areaId?: number;
        batchNo?: string;
        productionDate?: string;
        expirationDate?: string;
      }>;
    },
  ) {
    return request.post<void>({
      url: `/wms/receiptOrder/${id}/complete`,
      data,
    });
  }

  /**
   * 取消入库单
   */
  static cancelReceiptOrder(id: number, reason: string) {
    return request.post<void>({
      url: `/wms/receiptOrder/${id}/cancel`,
      data: { reason },
    });
  }

  /**
   * 打印入库单
   */
  static printReceiptOrder(id: number) {
    return request.get<Blob>({
      url: `/wms/receiptOrder/${id}/print`,
      responseType: "blob",
    });
  }
}

/**
 * 出库单API服务
 */
export class WmsShipmentOrderService {
  /**
   * 获取出库单列表
   */
  static getShipmentOrderList(params?: WMS.Order.OrderQueryParams) {
    return request.get<WMS.Order.ShipmentOrderListResponse>({
      url: "/wms/shipmentOrder/list",
      params,
    });
  }

  /**
   * 获取出库单详情
   */
  static getShipmentOrderDetail(id: number) {
    return request.get<WMS.Order.ShipmentOrderInfo>({
      url: `/wms/shipmentOrder/${id}`,
    });
  }

  /**
   * 创建出库单
   */
  static createShipmentOrder(
    data: Omit<
      WMS.Order.ShipmentOrderInfo,
      "id" | "orderNo" | "createTime" | "updateTime"
    >,
  ) {
    return request.post<WMS.Order.ShipmentOrderInfo>({
      url: "/wms/shipmentOrder",
      data,
    });
  }

  /**
   * 更新出库单
   */
  static updateShipmentOrder(
    id: number,
    data: Partial<WMS.Order.ShipmentOrderInfo>,
  ) {
    return request.put<WMS.Order.ShipmentOrderInfo>({
      url: `/wms/shipmentOrder/${id}`,
      data,
    });
  }

  /**
   * 删除出库单
   */
  static deleteShipmentOrder(id: number) {
    return request.del<void>({
      url: `/wms/shipmentOrder/${id}`,
    });
  }

  /**
   * 确认出库单
   */
  static confirmShipmentOrder(id: number) {
    return request.post<void>({
      url: `/wms/shipmentOrder/${id}/confirm`,
    });
  }

  /**
   * 分配库存
   */
  static allocateShipmentOrder(
    id: number,
    data: {
      allocationStrategy?: "FIFO" | "LIFO" | "FEFO";
      details?: Array<{
        detailId: number;
        allocations: Array<{
          inventoryDetailId: number;
          allocatedQuantity: number;
        }>;
      }>;
    },
  ) {
    return request.post<void>({
      url: `/wms/shipmentOrder/${id}/allocate`,
      data,
    });
  }

  /**
   * 开始拣货作业
   */
  static startPickingWork(id: number) {
    return request.post<void>({
      url: `/wms/shipmentOrder/${id}/picking/start`,
    });
  }

  /**
   * 完成拣货作业
   */
  static completePickingWork(
    id: number,
    data: {
      details: Array<{
        id: number;
        actualQuantity: number;
      }>;
    },
  ) {
    return request.post<void>({
      url: `/wms/shipmentOrder/${id}/picking/complete`,
      data,
    });
  }

  /**
   * 完成出库作业
   */
  static completeShipmentWork(id: number) {
    return request.post<void>({
      url: `/wms/shipmentOrder/${id}/complete`,
    });
  }

  /**
   * 取消出库单
   */
  static cancelShipmentOrder(id: number, reason: string) {
    return request.post<void>({
      url: `/wms/shipmentOrder/${id}/cancel`,
      data: { reason },
    });
  }

  /**
   * 打印出库单
   */
  static printShipmentOrder(id: number) {
    return request.get<Blob>({
      url: `/wms/shipmentOrder/${id}/print`,
      responseType: "blob",
    });
  }

  /**
   * 打印拣货单
   */
  static printPickingList(id: number) {
    return request.get<Blob>({
      url: `/wms/shipmentOrder/${id}/picking-list/print`,
      responseType: "blob",
    });
  }
}

/**
 * 移库单API服务
 */
export class WmsMovementOrderService {
  /**
   * 获取移库单列表
   */
  static getMovementOrderList(params?: WMS.Order.OrderQueryParams) {
    return request.get<WMS.Order.MovementOrderListResponse>({
      url: "/wms/movementOrder/list",
      params,
    });
  }

  /**
   * 获取移库单详情
   */
  static getMovementOrderDetail(id: number) {
    return request.get<WMS.Order.MovementOrderInfo>({
      url: `/wms/movementOrder/${id}`,
    });
  }

  /**
   * 创建移库单
   */
  static createMovementOrder(
    data: Omit<
      WMS.Order.MovementOrderInfo,
      "id" | "orderNo" | "createTime" | "updateTime"
    >,
  ) {
    return request.post<WMS.Order.MovementOrderInfo>({
      url: "/wms/movementOrder",
      data,
    });
  }

  /**
   * 更新移库单
   */
  static updateMovementOrder(
    id: number,
    data: Partial<WMS.Order.MovementOrderInfo>,
  ) {
    return request.put<WMS.Order.MovementOrderInfo>({
      url: `/wms/movementOrder/${id}`,
      data,
    });
  }

  /**
   * 删除移库单
   */
  static deleteMovementOrder(id: number) {
    return request.del<void>({
      url: `/wms/movementOrder/${id}`,
    });
  }

  /**
   * 确认移库单
   */
  static confirmMovementOrder(id: number) {
    return request.post<void>({
      url: `/wms/movementOrder/${id}/confirm`,
    });
  }

  /**
   * 开始移库作业
   */
  static startMovementWork(id: number) {
    return request.post<void>({
      url: `/wms/movementOrder/${id}/start`,
    });
  }

  /**
   * 完成移库作业
   */
  static completeMovementWork(
    id: number,
    data: {
      details: Array<{
        id: number;
        actualQuantity: number;
      }>;
    },
  ) {
    return request.post<void>({
      url: `/wms/movementOrder/${id}/complete`,
      data,
    });
  }

  /**
   * 取消移库单
   */
  static cancelMovementOrder(id: number, reason: string) {
    return request.post<void>({
      url: `/wms/movementOrder/${id}/cancel`,
      data: { reason },
    });
  }

  /**
   * 打印移库单
   */
  static printMovementOrder(id: number) {
    return request.get<Blob>({
      url: `/wms/movementOrder/${id}/print`,
      responseType: "blob",
    });
  }
}

/**
 * 盘点单API服务
 */
export class WmsCheckOrderService {
  /**
   * 获取盘点单列表
   */
  static getCheckOrderList(params?: WMS.Order.OrderQueryParams) {
    return request.get<WMS.Order.CheckOrderListResponse>({
      url: "/wms/checkOrder/list",
      params,
    });
  }

  /**
   * 获取盘点单详情
   */
  static getCheckOrderDetail(id: number) {
    return request.get<WMS.Order.CheckOrderInfo>({
      url: `/wms/checkOrder/${id}`,
    });
  }

  /**
   * 创建盘点单
   */
  static createCheckOrder(
    data: Omit<
      WMS.Order.CheckOrderInfo,
      "id" | "orderNo" | "createTime" | "updateTime"
    >,
  ) {
    return request.post<WMS.Order.CheckOrderInfo>({
      url: "/wms/checkOrder",
      data,
    });
  }

  /**
   * 更新盘点单
   */
  static updateCheckOrder(id: number, data: Partial<WMS.Order.CheckOrderInfo>) {
    return request.put<WMS.Order.CheckOrderInfo>({
      url: `/wms/checkOrder/${id}`,
      data,
    });
  }

  /**
   * 删除盘点单
   */
  static deleteCheckOrder(id: number) {
    return request.del<void>({
      url: `/wms/checkOrder/${id}`,
    });
  }

  /**
   * 确认盘点单
   */
  static confirmCheckOrder(id: number) {
    return request.post<void>({
      url: `/wms/checkOrder/${id}/confirm`,
    });
  }

  /**
   * 开始盘点作业
   */
  static startCheckWork(id: number) {
    return request.post<void>({
      url: `/wms/checkOrder/${id}/start`,
    });
  }

  /**
   * 提交盘点结果
   */
  static submitCheckResult(
    id: number,
    data: {
      details: Array<{
        id: number;
        actualQuantity: number;
        remark?: string;
      }>;
    },
  ) {
    return request.post<void>({
      url: `/wms/checkOrder/${id}/submit`,
      data,
    });
  }

  /**
   * 完成盘点作业
   */
  static completeCheckWork(id: number) {
    return request.post<void>({
      url: `/wms/checkOrder/${id}/complete`,
    });
  }

  /**
   * 取消盘点单
   */
  static cancelCheckOrder(id: number, reason: string) {
    return request.post<void>({
      url: `/wms/checkOrder/${id}/cancel`,
      data: { reason },
    });
  }

  /**
   * 打印盘点单
   */
  static printCheckOrder(id: number) {
    return request.get<Blob>({
      url: `/wms/checkOrder/${id}/print`,
      responseType: "blob",
    });
  }

  /**
   * 生成盘点差异报告
   */
  static generateDifferenceReport(id: number) {
    return request.get<Blob>({
      url: `/wms/checkOrder/${id}/difference-report`,
      responseType: "blob",
    });
  }
}

// 导出所有服务
export default {
  WmsOrderService,
  WmsReceiptOrderService,
  WmsShipmentOrderService,
  WmsMovementOrderService,
  WmsCheckOrderService,
};
